/*
 * Spinach.java
 *
 * Created on April 23, 2007, 6:28 PM
 *
 *  Life Logger v1.0
    Copyright (C) 2007  Anand Kishore (anand@semanticvoid.com)
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.semanticvoid.lifelogger;

import com.semanticvoid.lifelogger.configuration.ConfigManager;
import com.semanticvoid.lifelogger.configuration.Constants;
import com.semanticvoid.lifelogger.db.DbInterface;
import com.semanticvoid.lifelogger.entity.DataImporter;
import com.semanticvoid.lifelogger.entity.TaggedBrowseHistoryEntity;
import com.semanticvoid.lifelogger.entity.BrowseHistoryEntity;
import com.semanticvoid.lifelogger.entity.SearchHistoryEntity;
import com.semanticvoid.lifelogger.html.parser.HTMLParser;
import com.semanticvoid.lifelogger.http.ContentFetcher;
import com.semanticvoid.lifelogger.xml.parser.GoogleWebHistoryXMLReader;
import com.semanticvoid.lifelogger.xml.parser.DeliciousXMLReader;
import com.semanticvoid.lifelogger.xml.parser.GoogleReaderHistoryXMLReader;
import java.util.ArrayList;

import java.util.Vector;

/**
 * The command line driver class
 * @author anand kishore
 */
public class Spinach {
    
    /**
     * The main function
     * @param args the cmd line arguments
     */
    public static void main(String[] args) {
        
        // Check for atleast some arguments
        if(args.length < 1) {
            System.out.println("What the hell am I supposed to do?");
            System.out.println("For help type 'help' as an argument");
            System.exit(1);
        } else {
            
            // Print the help
            if(args[0].equalsIgnoreCase("help")) {
                printHelp();
            }
            
            if(!args[0].startsWith("-")) {
                System.out.println("Damn! Where are the options?");
                System.exit(1);
            }
            
            String[] options = args[0].split("");
            for(int i=1; i<options.length; i++) {
                if(options[i].equalsIgnoreCase("c")) {
                    if(args.length < 2) {
                        System.out.println("Hmmm...Okay Mr. Absent Minded, where is the configuration file path?");
                        System.exit(1);
                    } else {
                        ConfigManager.setConfigurationFilePath(args[1]);
                    }
                } else if(options[i].equalsIgnoreCase("d")) {
                    ConfigManager.setWriteToDb(true);
                } else if(options[i].equalsIgnoreCase("f")) {
                    ConfigManager.setFetchContent(true);
                }
            }
        }
        
        // Get instance of config manager
        ConfigManager mgr = ConfigManager.getInstance();
        
        if(mgr.isWriteToDb()) {
            DataImporter.importGoogleWebHistory();
            DataImporter.importDelicious();
            DataImporter.importGoogleReader();
        }
        
        DataImporter.fetchHTMLContent();
    }
    
    /**
     * Print the help
     */
    private static void printHelp() {
        System.out.println("Usage:");
        System.out.println("       java com.semantic....LifeLogger -c[options] <path to configuration file>");
        System.out.println("   c    -   configuration file is provided as an argument [NOT AN OPTION]");
        
        System.out.println("");
        System.out.println("Options: ");
        System.out.println("   d    -   write the entries to db");
        System.out.println("   f    -   fetch and write the html content to db");
        System.exit(1);
    }
    
    /**
     * Print status
     */
    private static void printProgress(double index, double size, double numOfUnits) {
        double unit = size / numOfUnits;
        double diff = Math.ceil(unit) - unit;
        
        double times = index / unit;
        
        if( (index == ((unit*times) + diff)) ) {
            System.out.print("#");
        }
    }
    
}
